package com.hqyj.manager.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.manager.service.ProductsService;
import com.hqyj.pojo.Category;
import com.hqyj.pojo.Order;
import com.hqyj.pojo.Product;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/manager")
public class GoodsController {
    @Value("D://IdeaProjects//springboot//springbootimage/")
    private  String serviceUrl;
    @Value("/upload/")
    private  String FilePath;
    @Autowired
    private ProductsService productsService;
   /*分页列表*/
    @RequestMapping("/goods/goodsList")
    public String goodsList(ModelMap modelMap,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "2") int pageSize){

        PageInfo<Product> pageInfo = productsService.findAllProduct(pageNum, pageSize);
        //查询商品分类
        List<Category> categoryList = productsService.slectCategory();
        modelMap.addAttribute("categoryList",categoryList);
        System.out.println(pageInfo.getList());
        modelMap.addAttribute("productList",pageInfo.getList());
        modelMap.addAttribute("productPageInfo",pageInfo);
        modelMap.addAttribute("totalNum",pageInfo.getTotal());//查询回显
        return "/manager/goods/goods-list";
    }

    /*搜索*/
    @RequestMapping("/goods/sreachList")
    public String sreachList(ModelMap modelMap,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "2") int pageSize,
                            @RequestParam(value = "categoryId", defaultValue= "1") int categoryId,
                            @RequestParam(value = "reviewCount", defaultValue = "0") int reviewCount,
                            @RequestParam(value = "searchStr", defaultValue =" ") String searchStr){
        //查询商品分类
        List<Category> categoryList = productsService.slectCategory();
        modelMap.addAttribute("categoryList",categoryList);
        System.out.println("进来的搜索内容"+categoryId+" " +reviewCount +" " +searchStr);
        PageInfo<Product> pageInfo = productsService.sreachProduct(pageNum, pageSize,categoryId,reviewCount,searchStr);
        System.out.println("我是搜索出啦的"+pageInfo.getList());
        modelMap.addAttribute("productList",pageInfo.getList());
        modelMap.addAttribute("productPageInfo",pageInfo);
        modelMap.addAttribute("inputName",searchStr);//查询回显
        modelMap.addAttribute("totalNum",pageInfo.getTotal());//查询回显
        return "/manager/goods/goods-list";
    }

    //分类列表
    @RequestMapping("/goods/Categorylist")
    public String Categorylist(Model model,@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "6") int pageSize,
                               @RequestParam(value = "sreachCategory", defaultValue = "") String sreachCategory) {
        PageInfo<Category> categoryList = productsService.slectCategory(pageNum, pageSize,sreachCategory);
        model.addAttribute("categoryPageInfo",categoryList);
        model.addAttribute("categoryList",categoryList.getList());
        model.addAttribute("totalNum",categoryList.getTotal());//查询回显

        return "/manager/goods/Categorylist";
    }

    /*商品增加页面*/
    @RequestMapping("/goods/productAdd")
    public String productAdd(Model model) {
        //查询商品分类
        List<Category> categoryList = productsService.slectCategory();
        model.addAttribute("categoryList",categoryList);
        return "/manager/goods/productAdd";
    }
    /*商品修改页面*/
    @RequestMapping("/goods/updateProduct")
    public String updateProduct(Model model,@Param("id") Integer id) {
        //查询商品信息
        Product product = productsService.sreachProductById(id);
        model.addAttribute("product",product);
        //查询商品分类
        List<Category> categoryList = productsService.slectCategory();
        model.addAttribute("categoryList",categoryList);
        return "/manager/goods/productUpdate";
    }

    /*分类修改页面*/
    @RequestMapping("/goods/CategoryUpdate")
    public String CategoryUpdate(Model model,@Param("id") Integer id) {
        //查询商品信息
        Category category = productsService.sreachCategory(id);
        System.out.println("商品："+category.toString());
        model.addAttribute("category",category);
        return "/manager/goods/CategoryUpdate";
    }

    /*分类增加页面*/
    @RequestMapping("/goods/categoryAddhtml")
    public String categoryAddhtml(Model model) {
        return "/manager/goods/categoryAdd";
    }

   /*文件上传页面*/
    @RequestMapping("/goods/upload")
    @ResponseBody
    public HashMap<String, Object> upload(MultipartFile[] file){
        HashMap<String, Object> map = new HashMap<>();
        try {
            if (file!=null&&file.length>0){
                for (int i = 0; i <file.length ; i++) {
                    String fileName = file[i].getOriginalFilename();
                    //截取点后面的字符
                    String oName = fileName.substring(fileName.lastIndexOf(".")+1);

                    //时间目录
                    SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
                    String timeDf = sdf.format(new Date());

                    //保证上传文件的唯一性
                    String fName = UUID.randomUUID() + "";

                    //拼接上传文件的全路径
                    String uploadName = serviceUrl + "\\" + timeDf + "\\" + fName +"."+ oName;

                    //创建文件对象
                    File f = new File(uploadName);
                    if (!f .exists()&&!f.isDirectory()){
                        f.mkdirs();
                    }
                    //保存文件
                    file[i].transferTo(f);

                    //把文件路径传到前台
                    map.put("src",FilePath+timeDf+"/"+fName +"."+ oName);
                    System.out.println("我是路径"+FilePath+timeDf+"/"+fName +"."+ oName);
                }
            }
            //获取文件的后缀

            //存0代表成功
            map.put("code",0);
            return map;

        } catch (IOException e) {
            e.printStackTrace();
        }

        map.put("src","");
        map.put("code",1);
        return map;
    }

    //单个商品删除
    @RequestMapping("/goods/del")
    @ResponseBody
    public Boolean del(@RequestParam("id") Integer id){
        return productsService.deleteProductById(id);
    }

    //单个类名删除
    @RequestMapping("/goods/categoryDel")
    @ResponseBody
    public Boolean categoryDel(@RequestParam("id") Integer id){
        return productsService.deleteCategoryById(id);
    }

    //批量删除类名
    @RequestMapping("/goods/deleteByIds")
    @ResponseBody
    public String deleteByIds(@RequestParam("oIds") String oIds){
        Integer integer = productsService.deleteByIds(oIds);
        if(integer>0){
            return "1";
        }
        return "0";
    }

    //批量删除商品
    @RequestMapping("/goods/deleteProductByIds")
    @ResponseBody
    public Boolean deleteProductByIds(@RequestParam("oIds") String oIds){
        return productsService.deleteProductByIds(oIds);
    }

    //单个类名增加
    @RequestMapping("/goods/categoryAdd")
    @ResponseBody
    public Boolean categoryAdd(@RequestParam("name") String name){
        System.out.println("我是类名："+name);
        return productsService.categoryAdd(name);
    }

    //单个商品的增加
    @RequestMapping("/goods/goodsAdd")
    @ResponseBody
    public  HashMap<String, Object> goodsAdd(Product product, @Param("url1") String url1,@Param("url2") String url2){
        System.out.println(product.toString());
        product.setCreateDate(new Date());
        product.setSaleCount(0);
        HashMap<String, Object> map = productsService.productAdd(product, url1, url2);
        return map;
    }

    //单个商品的修改
    @RequestMapping("/goods/goodsUpdate")
    @ResponseBody
    public  HashMap<String, Object> goodsUpdate(Product product, @Param("url1") String url1,@Param("url2") String url2){
        System.out.println(product.toString());
        HashMap<String, Object> map = productsService.productUpdate(product, url1, url2);
        return map;
    }

    //单个类名的修改
    @RequestMapping("/goods/categoryUpdateSave")
    @ResponseBody
    public  HashMap<String, Object> categoryUpdate(Category category){
        System.out.println(category.toString());
        HashMap<String, Object> map = productsService.categoryUpdate(category);
        return map;
    }


    //修改用户状态
    @RequestMapping("/goods/updateStatus")
    @ResponseBody
    public boolean updateStatus(@RequestParam("id") Integer id){
        System.out.println(id);
        return productsService.update(id);
    }

    //excel导出
    @RequestMapping("/excelWrite")
    public void excelWrite(HttpServletResponse response){

        productsService.excelWrite(response);
    }
}
